// 获取所有导航链接
const navLinks = document.querySelectorAll('.nav-link');
// 获取所有内容区域
const contentSections = document.querySelectorAll('.content-section');

// 为每个导航链接添加点击事件处理函数
navLinks.forEach((link) => {
    link.addEventListener('click', function (e) {
        e.preventDefault();

        // 移除所有导航链接的active类
        navLinks.forEach((navLink) => {
            navLink.classList.remove('active');
        });

        // 添加当前点击链接的active类
        this.classList.add('active');

        // 隐藏所有内容区域
        contentSections.forEach((contentSection) => {
            contentSection.classList.remove('active');
            contentSection.style.display = 'none';
        });

        // 获取目标内容区域的ID
        const targetId = this.dataset.target;

        // 根据ID查找目标内容区域并显示出来
        const targetSection = document.getElementById(targetId);
        if (targetSection) {
            targetSection.classList.add('active');
            targetSection.style.display = 'block';
        }
    });
});

document.addEventListener('DOMContentLoaded', function () {
    const roomSearchForm = document.getElementById('roomSearchForm');
    roomSearchForm.addEventListener('submit', function (e) {
        e.preventDefault();
        const formData = new FormData(roomSearchForm);
        const data = {};
        formData.forEach((value, key) => {
            data[key] = value;
        });
        const queryString = new URLSearchParams(data).toString();
        const url = `teacherfindRoom?${queryString}`;
        fetch(url, {
            method: 'GET',
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(rooms => {
                // const roomTable = document.getElementById('roomTable');
                // roomTable.innerHTML = '';
                rooms.forEach(room => {
                    const row = document.createElement('tr');
                    const classroomIdCell = document.createElement('td');
                    classroomIdCell.textContent = room.classroomId;
                    const classroomNameCell = document.createElement('td');
                    classroomNameCell.textContent = room.classroomName;
                    const capacityCell = document.createElement('td');
                    capacityCell.textContent = room.capacity;
                    const descriptionCell = document.createElement('td');
                    descriptionCell.textContent = room.description;
                    const statusCell = document.createElement('td');
                    statusCell.textContent = room.status;
                    const operationCell = document.createElement('td');
                    const applyButton = document.createElement('button');
                    applyButton.textContent = '申请';
                    applyButton.className = 'applyButton';
                    applyButton.dataset.classroomId = room.classroomId;
                    operationCell.appendChild(applyButton);
                    row.appendChild(classroomIdCell);
                    row.appendChild(classroomNameCell);
                    row.appendChild(capacityCell);
                    row.appendChild(descriptionCell);
                    row.appendChild(statusCell);
                    row.appendChild(operationCell);
                    roomTable.appendChild(row);
                });
            })
            .catch(error => {
                console.error('获取教室信息出错：', error);
            });
    });
});